package com.pearadmin.pro.modules.school.service.impl;

import java.util.List;
import java.util.ArrayList;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pearadmin.pro.common.tools.core.GeneralConvertUtils;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.base.page.Pageable;

import com.pearadmin.pro.modules.school.domain.DisciplineCategory;
import com.pearadmin.pro.modules.school.domain.ParticipantsProject;
import com.pearadmin.pro.modules.school.domain.School;
import com.pearadmin.pro.modules.school.dto.DisciplineCategoryDTO;
import com.pearadmin.pro.modules.school.param.DisciplineCategoryRequest;
import com.pearadmin.pro.modules.school.repository.DisciplineCategoryRepository;
import com.pearadmin.pro.modules.school.service.DisciplineCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 学科类别Service业务层处理
 *
 * @author ChenJian
 * @date 2021-08-29
 */
@Service
public class DisciplineCategoryServiceImpl extends ServiceImpl<DisciplineCategoryRepository, DisciplineCategory> implements DisciplineCategoryService
{
    @Resource
    private DisciplineCategoryRepository disciplineCategoryRepository;

    /**
     * 查询学科类别
     * @param request
     * @return 学科类别 分页集合
     * */
    @Override
    public PageResponse<DisciplineCategory> page(DisciplineCategoryRequest request)
    {
        return Pageable.of(request,(()->  List(request)));
    }

    @Override
    public List<DisciplineCategory> List(DisciplineCategoryRequest request) {
        LambdaQueryWrapper<DisciplineCategory> query = new LambdaQueryWrapper<DisciplineCategory>();
        query.eq(ObjectUtil.isNotNull(request.getDisciplineName()),DisciplineCategory::getDisciplineName,request.getDisciplineName());
        query.eq(ObjectUtil.isNotNull(request.getParentDiscipline()),DisciplineCategory::getParentDiscipline,request.getParentDiscipline());
        return  disciplineCategoryRepository.selectList(query);
    }

    /**
     * 查询学科类别
     * @return 学科类别 分页集合
     * */
    @Override
    public List<DisciplineCategoryDTO> tree()
    {
        DisciplineCategoryRequest request = new DisciplineCategoryRequest();
        request.setParentDiscipline(Long.getLong("0"));
        List<DisciplineCategoryDTO> pathList = GeneralConvertUtils.convert2List(List(request),DisciplineCategoryDTO.class);
        pathList.stream().forEach(d->{
            DisciplineCategoryRequest c = new DisciplineCategoryRequest();
            c.setParentDiscipline(d.getId());
            d.setChild(GeneralConvertUtils.convert2List(List(c),DisciplineCategoryDTO.class));
        });
        return pathList;
    }


    /**
     * 查询学科类别
     *
     * @param id 学科类别ID
     * @return 学科类别
     */
    @Override
    public DisciplineCategoryDTO selectDisciplineCategoryById(Long id)
    {
        DisciplineCategoryDTO disciplineCategoryDTO =  GeneralConvertUtils.conv(disciplineCategoryRepository.selectById(id),DisciplineCategoryDTO.class);
        if(ObjectUtils.isEmpty(disciplineCategoryDTO)&&ObjectUtils.isEmpty(disciplineCategoryDTO.getParentDiscipline())){
            DisciplineCategory disciplineCategory =   disciplineCategoryRepository.selectById(disciplineCategoryDTO.getParentDiscipline());
            if(ObjectUtils.isEmpty(disciplineCategory)){
                disciplineCategoryDTO.setParentDisciplineName(disciplineCategory.getDisciplineName());
            }
        }
        return disciplineCategoryDTO;
    }

}
